package com.isoft.listener;

import com.isoft.data.service.AllService;
import com.isoft.data.service.AllService2;
import com.isoft.data.util.ParseXmlUtil;
import com.isoft.data.xml.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

@Slf4j
@Component
/*
 * 创建文件改变监听器
 * */
public class FileAlterationalListener extends FileAlterationListenerAdaptor {
    @Autowired
    AllService allService;
    @Autowired
    AllService2 allService2;

    @Override
    public void onStop(FileAlterationObserver observer) {
        log.info("文件夹监听结束了......");
    }
    //如果新增的目录同时含有文件，在该回调方法下可以遍历除目录下的新增文件
    @Override
    public void onDirectoryCreate(File directory) {
        log.info("【创建了子目录-getName】 : " + directory.getName());
        File[] subFiles = directory.listFiles() ;
        for(File f : subFiles) {
            // 新增文件的路径 f.getAbsolutePath();
            log.info("新增目录" + directory.getName() + "下的文件 : " + f.getName());
        }
    }

    @Override
    public void onDirectoryChange(File directory) {
        log.info("目录发生了改变-getAbsolutePath:"+directory.getAbsolutePath());
    }

    @Override
    public void onDirectoryDelete(File directory) {
        log.info("删除目录-getPath"+directory.getPath());
    }

    @Override
    public void onFileChange(File file) {
        log.info("文件改变:"+file.getAbsolutePath());
    }
    //只有新增文件，不管是监听的父目录，还是子目录，都会回调该方法

    @Override
    public void onFileCreate(File file) {
        log.info("文件创建:"+file.getAbsolutePath());
        File[] subFiles=file.listFiles();
        for (File f:subFiles){
            String path=f.getAbsolutePath();
            switch (file.getName().substring(5,9)){
                case "AUPE":
                    try {
                        Apue order= ParseXmlUtil.parseFromFile(path,f.getName(),Apue.class);
                        allService.addApue(order);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "ARRE":
                    try {
                        Arre order=ParseXmlUtil.parseFromFile(path,f.getName(),Arre.class);
                        allService.addArre(order);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "BLLS":
                    try {
                        Blls order=ParseXmlUtil.parseFromFile(path,f.getName(),Blls.class);
                        allService.addBlls(order);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "BORE":
                    try {
                        Bore order=ParseXmlUtil.parseFromFile(path,f.getName(),Bore.class);
                        allService.addBore(order);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "CFCE":
                    try {
                        Cfce order=ParseXmlUtil.parseFromFile(path,f.getName(),Cfce.class);
                        allService.addCfce(order);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "CFUE":
                    try {
                        Cfue order=ParseXmlUtil.parseFromFile(path,f.getName(),Cfue.class);
                        allService.addCfue(order);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "CKIE":
                    try {
                        Ckie order=ParseXmlUtil.parseFromFile(path,f.getName(),Ckie.class);
                        allService.addCkie(order);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "CKLS":
                    try {
                        Ckls order=ParseXmlUtil.parseFromFile(path,f.getName(),Ckls.class);
                        allService.addCkls(order);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "CKOE":
                    try {
                        Ckoe order=ParseXmlUtil.parseFromFile(path,f.getName(),Ckoe.class);
                        allService.addCkoe(order);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "DEPE":
                    try {
                        DFME_DEPE order=ParseXmlUtil.parseFromFile(path,f.getName(),DFME_DEPE.class);
                        allService2.addDEPE(order);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "DFDE":
                    try {
                        Dfde order=ParseXmlUtil.parseFromFile(path,f.getName(),Dfde.class);
                        allService2.addDfde(order);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "DLYE":
                    try {
                        DFME_DLYE order=ParseXmlUtil.parseFromFile(path,f.getName(),DFME_DLYE.class);
                        allService2.addDLYE(order);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "FETT":
                    try {
                        DFME_FETT order=ParseXmlUtil.parseFromFile(path,f.getName(),DFME_FETT.class);
                        allService2.addFETT(order);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "POKE":
                    try {
                        Poke order=ParseXmlUtil.parseFromFile(path,f.getName(),Poke.class);
                        allService.addPoke(order);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
//                case "STLS":
//                    try {
//                        stls order=ParseXmlUtil.parseFromFile(path,f.getName(),Stls.class);
//                        allService.addSTLS(order);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                    break;
                default:
                    log.info("没有此文件");
            }
        }
    }

    @Override
    public void onFileDelete(File file) {
        log.info("文件删除:"+file.getAbsolutePath());
    }
}
